home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / Tool Chest / Development Platforms / AppsToGo / AppsToGo.src / FontTester / Menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-18  |  10.6 KB  |  401 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        Menu.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19.  
  20.  
  21. /*****************************************************************************/
  22.  
  23.  
  24.  
  25. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  26. #include "App.defs.h"        /* Get various application definitions.            */
  27. #include "App.protos.h"        /* Get the prototypes for application.            */
  28.  
  29. #ifndef __DESK__
  30. #include <Desk.h>
  31. #endif
  32.  
  33. #ifndef __ERRORS__
  34. #include <Errors.h>
  35. #endif
  36.  
  37. #ifndef __MEMORY__
  38. #include <Memory.h>
  39. #endif
  40.  
  41. #ifndef __MENUS__
  42. #include <Menus.h>
  43. #endif
  44.  
  45. #ifndef __TOOLUTILS__
  46. #include <ToolUtils.h>
  47. #endif
  48.  
  49. #ifndef __UTILITIES__
  50. #include "Utilities.h"
  51. #endif
  52.  
  53.  
  54.  
  55. /*****************************************************************************/
  56.  
  57.  
  58. extern Boolean    gQuitApplication;
  59. extern Boolean    gHasAppleEvents;
  60. extern OSType    gAppWindowType;
  61.  
  62. extern Boolean    gLowOnMem;
  63.  
  64.  
  65.  
  66. /*****************************************************************************/
  67. /*****************************************************************************/
  68.  
  69.  
  70.  
  71. /* •• Called by DTS.Lib..framework. •• */
  72.  
  73. /* Adjust the menu items.  We allow the DTS.Lib framework to do most of the work
  74. ** for us.  It will walk the menubar, and for each menu in the menubar, it will
  75. ** disable all of the menu items and then call the application at either
  76. ** AdjustMenuItems() (for document and palette windows, or for the no-window case),
  77. ** or DialogAdjustMenuItems() (for modal dialogs).  The application's job is to then
  78. ** turn on menu items that should be enabled to match the current application state.
  79. ** The initial Wannabe code for AdjustMenuItems() calls DoAdjustFileMenu() for the
  80. ** file menu, and DoAdjustEditMenu() for the edit menu.  Any other menus that may
  81. ** be added to Wannabe have all menu items enabled.  This allows menus to be added
  82. ** to the running version of Wannabe and allows them to actually do something.
  83. ** If the top-most window is a dialog, then all menus are disabled except for the
  84. ** edit menu.  Various items in the edit menu are enabled, depending on if there
  85. ** is an active TextEdit control, and what is in the clipboard. */
  86.  
  87. #pragma segment Menu
  88. void    DoAdjustMenus(void)
  89. {
  90.     if (DoAdjustMBARMenus(FrontWindow(), rMenuBar))
  91.         DrawMenuBar();
  92. }
  93.  
  94.  
  95.  
  96. /*****************************************************************************/
  97.  
  98.  
  99.  
  100. /* This is called when an item is chosen from the menu bar (after calling
  101. ** MenuSelect or MenuKey).  It performs the right operation for each command.
  102. ** It is good to have both the result of MenuSelect and MenuKey go to one
  103. ** routine like this to keep everything organized. */
  104.  
  105. #pragma segment Menu
  106. Boolean    DoMenuCommand(short menuID, short menuItem)
  107. {
  108.     short        undoDepth, numUndos, saveMode, daRefNum, i;
  109.     Str255        daName;
  110.     FileRecHndl    frHndl, ff;
  111.     WindowPtr    window, ww;
  112.     TEHandle    te;
  113.     OSErr        err;
  114.     Boolean        handled;
  115.     MenuHandle    menu;
  116.     Str255        str, fname;
  117.     
  118.     handled = true;
  119.  
  120.     if (window = FrontWindow())
  121.         frHndl = (FileRecHndl)GetWRefCon(window);
  122.             /* frHndl is valid only if it is one of our windows. */
  123.  
  124.     switch (menuID) {
  125.  
  126.         case mApple:
  127.             switch (menuItem) {
  128.                 case kStdAbout:        /* Bring up alert for About. */
  129.                     NewDocumentWindow(nil, 'ABOT', false);
  130.                     break;
  131.                 default:            /* All non-About items in this menu are DAs. */
  132.                     GetItem(GetMHandle(mApple), menuItem, daName);
  133.                     daRefNum = OpenDeskAcc(daName);
  134.                     break;
  135.             }
  136.             break;
  137.  
  138.         case mFile:
  139.             switch (menuItem) {
  140.                 case kStdNew:
  141.                     if (err = NewDocumentWindow(nil, gAppWindowType, true))
  142.                         HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  143.                     break;
  144.                 case kStdOpen:
  145.                     err = OpenDocumentWindow(&frHndl, nil, fsRdWrPerm);
  146.                     if ((err) && (err != userCanceledErr))
  147.                         HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  148.                     break;
  149.                 case kStdClose:
  150.                     if (IsAppWindow(window)) {
  151.                         if (window = FrontWindowOfType(kwIsDocument, true))
  152.                             DisposeOneWindow(window, kClose);
  153.                     }
  154.                     else
  155.                         DisposeOneWindow(window, kClose);        /* Dispose of DA window. */
  156.                     break;
  157.                 case kStdSave:
  158.                 case kStdSaveAs:
  159.                     saveMode = (menuItem == kStdSave) ? kSave : kSaveAs;
  160.                     if ((*frHndl)->fileState.refNum == kInvalRefNum)
  161.                         saveMode = kSaveAs;
  162.                     err = SaveDocument(frHndl, window, saveMode);
  163.                     if ((err) && (err != userCanceledErr))
  164.                         HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  165.                     break;
  166.                 case kStdPageSetup:
  167.                     DoSetCursor(&qd.arrow);
  168.                     PresentStyleDialog(frHndl);
  169.                     break;
  170.                 case kStdPrint:
  171.                     DoSetCursor(&qd.arrow);
  172.                     err = noErr;
  173.                     if (!(*frHndl)->d.doc.fhInfo.printRecValid)
  174.                         err = PresentStyleDialog(frHndl);
  175.                     if (!err) {
  176.                         err = PrintDocument(frHndl, true, true);
  177.                         PrintDocument(nil, false, false);
  178.                     }
  179.                     if ((err) && (err != userCanceledErr))
  180.                         HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  181.                     break;
  182.                 case kStdQuit:
  183.                     gQuitApplication = DisposeAllWindows();
  184.                     break;
  185.                 default:
  186.                     handled = false;
  187.                     break;
  188.             }
  189.             break;
  190.  
  191.         case mEdit:            /* Call SystemEdit for DA editing & MultiFinder. */
  192.             if (menuItem == kStdViewHier) {
  193.                 handled = false;
  194.                 break;
  195.             }
  196.             if (IsAppWindow(window)) {
  197.                 switch (menuItem) {
  198.                     case kStdUndo:
  199.                     case kStdRedo:
  200.                     case kStdCut:
  201.                     case kStdCopy:
  202.                     case kStdPaste:
  203.                     case kStdClear:
  204.                         switch ((*frHndl)->fileState.sfType) {
  205.                                 /* This is written with the assumption that document types
  206.                                 ** that demand specific code will be added.  The below “if”
  207.                                 ** illustrates how to handle the edit menu for windows that
  208.                                 ** have an active TextEdit control.  The “else” shows a typical
  209.                                 ** undo/redo scenario for applications that are using the
  210.                                 ** hierarchical document package.  The clipboard features
  211.                                 ** are of course document-dependent, so a sample hasn't been
  212.                                 ** implemented here.  For a sample, see DTS.Draw. */
  213.                             default:
  214.                                 if (te = CTEFindActive(window)) {
  215.                                     if ((*te)->viewRect.left < -8192)
  216.                                         BeginFrame(window);
  217.                                     else
  218.                                         BeginContent(window);
  219.                                     if (menuItem == kStdUndo)
  220.                                         CTEUndo();
  221.                                     else
  222.                                         CTEClipboard(menuItem - kStdCut + 2);
  223.                                     EndContent(window);
  224.                                 }
  225.                                 else {
  226.                                     if (menuItem <= kStdRedo) {
  227.                                         if (!UnmapMItem(mEdit, kStdUndo)) {
  228.                                             GetUndoInfo(frHndl, &undoDepth, &numUndos);
  229.                                             DoUndoTask((*frHndl)->d.doc.root, 1 - undoDepth, true);
  230.                                         }
  231.                                         else DoUndoTask((*frHndl)->d.doc.root, menuItem - kStdUndo, true);
  232.                                     }
  233.                                     else {
  234.                                         /* Handle rest of edit menu here. */
  235.                                     }
  236.                                 }
  237.                                 break;
  238.                         }
  239.                         break;
  240.                 }
  241.             }
  242.             else SystemEdit(menuItem - 1);
  243.             break;
  244.  
  245.         case mFont:
  246.             menu = GetMHandle(mFont);
  247.             GetItem(menu, menuItem, str);
  248.             ww = GetNextWindow(nil, kDocFileType);
  249.             if (ww) {
  250.                 ff = (FileRecHndl)GetWRefCon(ww);
  251.                 if (pcmp((*ff)->d.doc.fontName, str)) {
  252.                     (*ff)->d.doc.newImage = true;
  253.                     pcpy((*ff)->d.doc.fontName, str);
  254.                     BeginContent(ww);
  255.                     DoImageDocument(ff);
  256.                     EndContent(ww);
  257.                     SetDocSize(ff, kwNoChange, (*ff)->d.doc.docSize);
  258.                 }
  259.             }
  260.             GetItem(menu, menuItem, fname);
  261.             for (i = CountMItems(menu); i; --i) {
  262.                 GetItem(menu, i, str);
  263.                 if (pcmp(fname, str)) CheckItem(menu, i, false);
  264.                 else                  CheckItem(menu, i, true);
  265.             }
  266.             break;
  267.  
  268.         default:
  269.             handled = false;
  270.             break;
  271.  
  272.     }
  273.  
  274.     return(handled);
  275. }
  276.  
  277.  
  278.  
  279. /*****************************************************************************/
  280.  
  281.  
  282.  
  283. #pragma segment Menu
  284. Boolean    DoAdjustFileMenu(WindowPtr window)
  285. {
  286.     MenuHandle    menu;
  287.     FileRecHndl    frHndl;
  288.     short        enableItem;
  289.  
  290.     menu = GetMHandle(mFile);
  291.     EnableItem(menu, UnmapMItem(mFile, kStdQuit));            /* Gotta be able to quit. */
  292.  
  293.     if (IsDAWindow(window)) {
  294.         EnableItem(menu, UnmapMItem(mFile, kStdClose));        /* Let DAs do a close from the menu. */
  295.         return(false);
  296.     }
  297.  
  298.     if (!gLowOnMem) {
  299.         EnableItem(menu, UnmapMItem(mFile, kStdNew));
  300.         EnableItem(menu, UnmapMItem(mFile, kStdOpen));
  301.     }
  302.  
  303.     if (window = FrontWindowOfType(kwIsDocument, true)) {
  304.         EnableItem(menu, UnmapMItem(mFile, kStdClose));
  305.         frHndl = (FileRecHndl)GetWRefCon(window);
  306.         if ((*frHndl)->fileState.sfType == kDocFileType) {
  307.             enableItem = GetWindowDirty(window);
  308.             if ((*frHndl)->fileState.refNum == kInvalRefNum)
  309.                 enableItem = true;
  310.             if (enableItem)
  311.                 EnableItem(menu, UnmapMItem(mFile, kStdSave));
  312.             EnableItem(menu, UnmapMItem(mFile, kStdSaveAs));
  313.         }
  314.         EnableItem(menu, UnmapMItem(mFile, kStdPageSetup));
  315.         EnableItem(menu, UnmapMItem(mFile, kStdPrint));
  316.     }
  317.  
  318.     return(false);
  319. }
  320.  
  321.  
  322.  
  323. /*****************************************************************************/
  324.  
  325.  
  326.  
  327. #pragma segment Menu
  328. Boolean    DoAdjustEditMenu(WindowPtr window)
  329. {
  330.     MenuHandle        menu;
  331.     Boolean            menuEnabled;
  332.     FileRecHndl        frHndl;
  333.  
  334.     menu = GetMHandle(mEdit);
  335.  
  336.     if (IsDAWindow(window)) {
  337.         EnableItem(menu, UnmapMItem(mEdit, kStdUndo));
  338.         EnableItem(menu, UnmapMItem(mEdit, kStdCut));
  339.         EnableItem(menu, UnmapMItem(mEdit, kStdCopy));
  340.         EnableItem(menu, UnmapMItem(mEdit, kStdPaste));
  341.         EnableItem(menu, UnmapMItem(mEdit, kStdClear));
  342.         return(false);
  343.     }
  344.  
  345.     if (IsAppWindow(window)) {
  346.         frHndl = (FileRecHndl)GetWRefCon(window);
  347.         switch ((*frHndl)->fileState.sfType) {
  348. #if VH_VERSION
  349.             case kViewHierFileType:
  350.                 CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
  351.                                                  UnmapMItem(mEdit, kStdCut));
  352.                 break;
  353. #endif
  354.             default:
  355.                 EnableItem(menu, UnmapMItem(mEdit, kStdViewHier));
  356.                 CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
  357.                                                  UnmapMItem(mEdit, kStdCut));
  358.                 break;
  359.         }
  360.     }
  361.  
  362.     return(false);
  363. }
  364.  
  365.  
  366.  
  367. /*****************************************************************************/
  368.  
  369.  
  370.  
  371. #pragma segment Menu
  372. Boolean    DoAdjustFontMenu(WindowPtr window);
  373. Boolean    DoAdjustFontMenu(WindowPtr window)
  374. {
  375. #pragma unused (window)
  376.  
  377.     MenuHandle    menu;
  378.     FileRecHndl    frHndl;
  379.     short        i;
  380.     Str255        fname, str;
  381.  
  382.     menu = GetMHandle(mFont);
  383.  
  384.     (*menu)->enableFlags |= 0xFFFFFFFEL;
  385.  
  386.     frHndl = GetNextDocument(nil, kDocFileType);
  387.     if (!frHndl) return(false);
  388.  
  389.     pcpy(fname, (*frHndl)->d.doc.fontName);
  390.     for (i = CountMItems(menu); i; --i) {
  391.         GetItem(menu, i, str);
  392.         if (pcmp(fname, str)) CheckItem(menu, i, false);
  393.         else                  CheckItem(menu, i, true);
  394.     }
  395.  
  396.     return(false);
  397. }
  398.  
  399.  
  400.  
  401.